package sample.stormax.pres.news.fragments;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


import sample.stormax.pres.news.R;
import sample.stormax.pres.news.adapters.NewsAdapter;
import sample.stormax.pres.news.beans.News;
import sample.stormax.pres.news.beans.Result;
import sample.stormax.pres.news.utils.http.HttpUtils;
import sample.stormax.pres.news.utils.http.JsonUtils;
import sample.stormax.pres.news.utils.sqlite.ISQLiteOperation;

public class NewsFragment extends Fragment {
    private static String TAG;

    //bundle key while create a newInstance
    private static final String TYPE = "type";

    //specific type of this fragment
    private String type;

    private RecyclerView recyclerView;
    private NewsAdapter adapter;

    //新闻数据加载
    private NewsAsyTask newsAsyTask;

    //当前视图
    private View view;

    public NewsFragment() {
    }

    public static NewsFragment newInstance(String type) {
        TAG = "NewsFragment_" + type;
        Log.d(TAG, "newInstance");
        NewsFragment newsFragment = new NewsFragment();
        Bundle args = new Bundle();
        args.putString(TYPE, type);
        newsFragment.setArguments(args);
        return newsFragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        Log.d(TAG, "onCreate");
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            type = getArguments().getString(TYPE);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        Log.d(TAG, "onCreateView");
        if (view == null) {
            view = inflater.inflate(R.layout.list_news, container, false);
            recyclerView = (RecyclerView) view;
            recyclerView.setLayoutManager(new LinearLayoutManager(view.getContext()));
            recyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL));
            setAdapter();
        }

        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (view != null) {
            ((ViewGroup) view.getParent()).removeView(view);
        }
    }

    private class NewsAsyTask extends AsyncTask<String, Void, Result> {

        @Override
        protected Result doInBackground(String... strings) {
            String json = HttpUtils.sendRequest(strings[0]);
            News news;
            Result result = null;
            if (!json.equals(HttpUtils.ERROR_TIMEOUT)) {
                news = JsonUtils.readValue(json, News.class);
                if (news != null) {
                    result = news.getResult();
                }
            }
            return result;
        }

        @Override
        protected void onPostExecute(Result result) {
            adapter = NewsAdapter.getNewsAdapter(getActivity(), result.getData(),type);
            recyclerView.setAdapter(adapter);
        }
    }

    private NewsAdapter setAdapter() {
        if (adapter == null) {
            Log.d(TAG, "new Adapter");
            newsAsyTask = new NewsAsyTask();
            newsAsyTask.execute("http://v.juhe.cn/toutiao/index?type=" + type + "&key=5465c4c5d60f72c3d756a9f1a9b8437d");
        } else {
            Log.d(TAG, "reuse Adapter");
        }
        return adapter;
    }
}
